package org.example.demoarticle.Controller;

import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.example.demoarticle.Service.UserService;
import org.example.demoarticle.Service.impl.UserServiceImpl;
import org.example.demoarticle.pojo.ResultByLogin;
import org.example.demoarticle.pojo.User;
import org.example.demoarticle.util.jwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/zut")
public class LoginController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public ResultByLogin Login(@RequestBody User user) {
        log.info("用户登录：()", user);
        User user2 = userService.login(user);
        if(user2!=null) {
            Map<String,Object> claims=new HashMap<>();
            claims.put("userName",user2.getUsername());
            claims.put("password",user2.getPassword());
            String jwt= jwtUtil.generateJwt(claims);
            return ResultByLogin.success(jwt,user2.username);
        }
        return ResultByLogin.error("用户名或密码错误");
    }
}
